package com.sdkx.app.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import com.sdkx.app.R;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.util.ToastUtil;
import com.sdkx.app.util.Util;
import com.sdkx.app.view.DegreeInputView;

public class AddWpWindow extends Dialog implements View.OnClickListener {
    Context context;
    private WayPoint tempWP = new WayPoint();
    private EditText editRemarks, wpname;
    private DegreeInputView lonedit, latedit;
    private Button btn_Confirm, btn_Cancel;
    private String title;
    private IWayPointCallback iWayPointCallback;
    boolean edit = false;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                tempWP.setLatitude(latedit.toDegree());
                tempWP.setLongitude(lonedit.toDegree());
                tempWP.setType(WayPoint.WayPointType.PERSON_POINT.ordinal());
                tempWP.setName(wpname.getText().toString());
                WayPointManager.filleditem(tempWP);
                if (tempWP.getName().equals("")) {
                    ToastUtil.show(context, "名称不能为空", 1000);
                } else {
                    if (WayPointManager.getSame(tempWP.getName())) {
                        ToastUtil.show(context, "航点名称重复！", 1000);
                    } else {
                        if (WayPointManager.insert(tempWP)) {
                            if (iWayPointCallback != null) {
                                if (!edit)
                                    iWayPointCallback.onAdd(tempWP);
                                else
                                    iWayPointCallback.onUpdate(tempWP);
                            }
                            dismiss();
                        } else {
                            ToastUtil.show(context, "保存失败！", 5);
                        }
                    }

                }

                break;
            case R.id.cancel:
                //TODO implement
                dismiss();
                break;
        }
    }

    public AddWpWindow(Activity activity) {
        super(activity);
        context = activity;
        init();

    }

    public AddWpWindow(Activity activity, WayPoint point) {
        super(activity);
        context = activity;
        tempWP = point;
        edit = true;
        init();
        wpname.setText(tempWP.getName());
        latedit.setTextEx(Util.degreePatten(getContext(),tempWP.getLatitude()));
        lonedit.setTextEx(Util.degreePatten(getContext(),tempWP.getLongitude()));
        editRemarks.setText("");
    }

    public void init() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(getContext().getResources().getColor(android.R.color.transparent)));
        View contentview = getLayoutInflater().inflate(R.layout.dlg_add_waypoint, null);
        wpname = contentview.findViewById(R.id.edit_wpname);
        latedit = contentview.findViewById(R.id.edit_lat);
        lonedit = contentview.findViewById(R.id.lon_edit);
        editRemarks = contentview.findViewById(R.id.edit_remarks);
        btn_Confirm = (Button) contentview.findViewById(R.id.confirm);
        btn_Cancel = (Button) contentview.findViewById(R.id.cancel);
        btn_Confirm.setOnClickListener(this);
        btn_Cancel.setOnClickListener(this);
        setContentView(contentview);
    }

    public void setTitleName(String name) {
        this.title = name;
    }

    public void setCallback(IWayPointCallback callback) {
        iWayPointCallback = callback;
    }

    public interface IWayPointCallback {
        void onAdd(WayPoint point);

        void onUpdate(WayPoint point);
    }

}